package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.CategoryMapper;
import com.code.dao.BrandMapper;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.Category;
import com.code.pojo.Brand;
import com.code.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper productMapper;


    @Override
    public PageResult<Brand> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Brand> brandList = brandMapper.findLike(queryPageBean);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        PageResult<Brand> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Brand> findPage(Brand brand) {
//     PageHelper.startPage(brand.getPage(),brand.getSize());
//        List<Brand> brandList = brandMapper.findLike(brand);
//        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
//        PageResult<Brand> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Category> findCategory() {
        return productMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return brandMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return brandMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Brand> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Brand> brandList = brandMapper.findPageNo(queryPageBean);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        PageResult<Brand> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        brand.setShow(1);
        brandMapper.updateByPrimaryKeySelective(brand);
    }
}
